//
//  ClientDiscussModel.m
//  BCVersion
//
//  Created by 吴 崧 on 14-2-24.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "ClientCommentModel.h"

@implementation ClientCommentModel

+(NSArray *)modelsWithDataSource:(NSArray *)dataSource
{
    __block NSMutableArray * result = [[NSMutableArray alloc]initWithCapacity:dataSource.count];
    [dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        __weak NSDictionary *infoDic = (NSDictionary *)obj;
        ClientCommentModel *comment = [[ClientCommentModel alloc]initWithDataSource:[infoDic copy]];
        [result addObject:comment];
    }];
    
    __autoreleasing NSArray *finalArray = [[NSArray alloc]initWithArray:[result copy]];
    result = nil;
    dataSource = nil;
    return finalArray;
}


-(instancetype)initWithDataSource:(NSDictionary *)dataSource
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dataSource];
    }
    return self;
}

-(void)setMessage:(NSString *)message
{
    _message = message;
    CGSize labelSize = [self string:message
                       sizeWithFont:[UIFont systemFontOfSize:11]
                 constrainedToWidth:307];
    self.lableHeight = labelSize.height;
}

-(void)setLableHeight:(CGFloat)lableHeight
{
    _lableHeight = lableHeight;
    self.cellHeight = 30 + lableHeight;
}

- (CGSize)string:(NSString *)string
    sizeWithFont:(UIFont *)font
constrainedToWidth:(CGFloat)targetWidth {
	CGSize maxSize = CGSizeMake(targetWidth, 0xF000);
	if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
		CGRect rect =  [string boundingRectWithSize:maxSize
		                                  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
		                               attributes:@{ NSFontAttributeName:font }
		                                  context:nil];
        
		return CGSizeCeil(rect.size);
	} else {
		return [string    sizeWithFont:font
		           constrainedToSize:maxSize
		               lineBreakMode:NSLineBreakByWordWrapping];
	}
}


@end
